package com.russell.pms.webapp.controller.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.russell.pms.exception.PMSException;
import com.russell.pms.webapp.bean.StatusMessage;

@ControllerAdvice
public class GlobalExceptionHandler {
	protected transient final Logger logger = LoggerFactory
			.getLogger(getClass());

	@ExceptionHandler(Exception.class)
	public ResponseEntity<String> handlePMSException(Exception ex) {
		logger.error("Exception " + ex.getMessage());
		return new ResponseEntity<String>("UNKNOWN ERROR",
				HttpStatus.INTERNAL_SERVER_ERROR);
	}

	@ExceptionHandler(PMSException.class)
	public ResponseEntity<StatusMessage> handleShipstarException(PMSException ex) {
		logger.info("Shipstar Exception " + ex.getMessage());
		StatusMessage e = new StatusMessage();
		e.setStatus(ex.getCode().getHttpStatus().value());
		e.setCode(ex.getCode().getCode());
		e.setMessage(ex.getMessage());

		return new ResponseEntity<StatusMessage>(e, ex.getCode()
				.getHttpStatus());
	}
}
